package com.yaoran.userauth.controller;

import com.yaoran.userauth.entity.Role;
import com.yaoran.userauth.service.RoleService;
import com.yaoran.userauth.utils.PageUtils;
import com.yaoran.userauth.utils.Query;
import com.yaoran.userauth.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    private final static Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public R roleAdd(@RequestBody Role role){
        roleService.addRole(role,null);
        return R.ok();
    }

    @ApiOperation(value = "查询列表")
    @PostMapping("/list")
    public R<PageUtils<Role>> listRole(@RequestBody Map<String,String> params){
        Map<String,Object> query = new HashMap<>();
        query.put("page",params.get("page"));
        query.put("limit",params.get("limit"));
        String keyWord = params.get("keyWord");
        PageUtils<Role> pageUtils = roleService.listRole(new Query(query),keyWord);
        return R.ok(pageUtils);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        return  R.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/del")
    public R deleteRole(@RequestBody Map<String,Long> params){
        roleService.deleteRole(params.get("id"));
        return R.ok();
    }

    @ApiOperation("批量删除")
    @PostMapping("/dels")
    public R deleteManyRole(@RequestBody Map<String,Long[]> params){
        roleService.deleteManyRole(params.get("ids"));
        return R.ok();
    }

    @ApiOperation("角色详情")
    @GetMapping("/detail")
    public R<Role> getRole(@RequestParam Long id){
        Role role = roleService.getRole(id);
        return  R.ok(role);
    }

    @ApiOperation("角色列表，不分页")
    @GetMapping("/listAll")
    public R<List<Role>> listAllRole(){
        List<Role> list = roleService.listAllRole();
        return R.ok(list);
    }
}
